<template>
  <div class="add">
    <el-dialog :title="{add:'新增学科',deit:'编辑学科'}[mode]" :visible.sync="show" width="600px" @close="closeEvent">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" />
        </el-form-item>
        <el-form-item label="角色" prop="role_id">
          <el-select v-model="form.role_id">
            <el-option
              v-for="item in statusList.role_id"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in statusList.statusList"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="show=false">取消</el-button>
          <el-button type="primary" @click="submit">确认</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { userAdd, userEdit } from '@/api/userList'
import statusList from '@/csontant/user'
export default {
  props: {
    value: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      mode: 'add',
      statusList,
      show: false,
      form: {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //
      },
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        email: [
          { required: true, message: '必填', trigger: 'change' },
          {
            pattern: '^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$',
            message: '邮箱格式错误',
            trigger: 'change'
          }
        ],
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 11, max: 11, // pattern: '/^1[345789]\d{9}$/',
            message: '手机号格式错误',
            trigger: 'change'
          }
        ],
        role_id: [{ required: true, message: '必填', trigger: 'change' }],
        status: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    // 点击确认
    async submit() {
      await this.$refs.form.validate()
      if (this.mode === 'add') {
        const res = await userAdd(this.form)
        this.$message.success(res.message)
      } else if (this.mode === 'deit') {
        await userEdit(this.form)
        this.$message.success('编辑学科')
      }
      this.show = false
      this.$emit('getData')
      this.closeEvent()
    },
    // 点击取消
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        username: '', //	是	string	用户名
        email: '', //	是	string	邮箱
        phone: '', //	是	string	手机号
        role_id: '', //	是	string	角色 、2 管理员、3 老师、4 学生
        status: '', //	否	string	1(启用)0(禁用)
        remark: '' //
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
